<template>
  <!-- 维护弹框 -->
  <el-dialog title="执行返利" width="720px" :visible.sync="visible" :before-close="beforeClose" @opened="dialogOpened">
    <table-box
      ref="refTable"
      v-loading="loading"
      no-request
      :data="tableData"
      :table-height="300"
      :show-custom-setting="false"
      :table-config="settingConfig"
    >
      <template>
        <el-table-column type="index" label="序号" />
        <el-table-column label="任务编码" prop="modelRoomTaskCode" />
        <el-table-column label="任务名称" prop="modelRoomTaskName" />
        <el-table-column label="提报月份" prop="submissionDate" />
        <el-table-column label="小区名称" prop="communityName" />
        <el-table-column label="样板间楼栋号" prop="storiedBuildingNumber" width="100" />
        <el-table-column label="是否执行返利" prop="isExecuteRebate" width="100">
          <template slot-scope="{row}">
            <el-switch
              v-model="row.isExecuteRebate"
              active-color="#3DC094"
              inactive-color="#d9d9d9"
              :active-value="1"
              :inactive-value="0"
            />
          </template>
        </el-table-column>
        <el-table-column label="返利执行时间" prop="rebateExecuteTime" width="260">
          <template slot-scope="{row}">
            <el-date-picker
              v-if="row.isExecuteRebate"
              v-model="row.rebateExecuteTime"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
            />
          </template>
        </el-table-column>
        <el-table-column label="执行返利说明" prop="executeRebateMsg" width="120">
          <template slot-scope="{row}">
            <el-input v-if="row.isExecuteRebate" v-model="row.executeRebateMsg" />
          </template>
        </el-table-column>
        <el-table-column label="未执行返利原因" prop="notExecuteRebateMsg" width="120">
          <template slot-scope="{row}">
            <el-input v-if="!row.isExecuteRebate" v-model="row.notExecuteRebateMsg" />
          </template>
        </el-table-column>
      </template>
    </table-box>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="beforeClose">取 消</el-button>
      <el-button v-loading="loading" size="small" type="primary" @click="handleOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { executeRebate } from '@/api/showRoomInfo'
export default {
  props: {
    visible: {
      type: Boolean,
      default() {
        return false
      }
    },
    rows: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      tableData: [],
      midData: [],
      settingConfig: {
        hideSearchBar: true,
        sort: '',
        buttonGroupCode: 'no',
        interfacePath: ''
      }
    }
  },
  created() {
    this.tableData = JSON.parse(JSON.stringify(this.rows))
  },
  methods: {
    cancel() {
      this.midData = []
    },
    handleCancel() {

    },
    async handleOk() {
      const params = this.tableData

      for (let i = 0; i < params.length; i++) {
        const item = params[i]
        if (item.isExecuteRebate === 1 && !item.rebateExecuteTime) {
          this.$message.warning('请检查返利时间填写完整')
          return false
        }
        if (item.isExecuteRebate === 0 && !item.notExecuteRebateMsg) {
          this.$message.warning('请检查未执行返利原因填写完整')
          return false
        }
      }
      this.loading = true
      try {
        const res = await executeRebate(params)
        if (res.success) {
          this.$message.success('执行成功')
          this.beforeClose(true)
        } else {
          this.$message.warning(res.msg)
        }
        this.loading = false
      } catch (error) {
        this.loading = false
      }
    },
    dialogOpened() {
      this.$refs.refTable.resetLayout()
    },
    beforeClose(flag) {
      this.$emit('close', flag)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
